<%-- 
    Document   : list
    Created on : Nov 16, 2014, 1:27:34 PM
    Author     : Hai
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link href="css/table.css" rel="stylesheet" type="text/css"/>
        <link href="css/pagination.css" rel="stylesheet" type="text/css"/>
        <link href="Theme/css/Menu.css" rel="stylesheet" type="text/css"/>
        <title>JSP Page</title>
    </head>
    <body>
        <%@include file="header.jsp" %>
        <h1>View!</h1>
        <button type="button" value="New Post"><a href="/post.jsp">New Post</a></button>

        <section>
            <nav class="pagination pagination-blue">

                <table class="table-fill">
                    <thead>
                        <tr>
                            <th class='text-center'>ID</th>
                            <th class='text-center'>Title</th>
                            <th class='text-center'>Posted by</th>
                            <th class='text-center'>Day</th>
                        </tr>
                    </thead>

                    <tbody>

                        <%  String sTable = request.getParameter("where");
                            String sPage = request.getParameter("p");
                            int numPage = 1;

                            if (sPage != null) {
                                numPage = Integer.parseInt(sPage);
                            }

                            String data = Database.GetData.Table(sTable, numPage);

                            String[] row = data.split("/n");

                            for (String val : row) {
                                String[] col = val.split("/r");
                                if (col.length > 1) {
                                    out.print("<tr>");
                                    out.print("<td class='text-center'><a href='/view.jsp?where=" + sTable + "&id=" + col[0] + "'>" + col[0] + "</a></td>");
                                    out.print("<td class='text-left'><a href='/view.jsp?where=" + sTable + "&id=" + col[0] + "'>" + col[1] + "</a></td>");
                                    out.print("<td class='text-left'><a href='/view.jsp?where=" + sTable + "&id=" + col[0] + "'>" + Database.GetData.id2Name(col[2]) + "</a></td>");
                                    out.print("<td class='text-text-'><a href='/view.jsp?where=" + sTable + "&id=" + col[0] + "'>" + col[3] + "</a></td>");
                                    out.print("</tr>");
                                }
                            }

                        %>
                    </tbody>
                </table>


                <a href=
                   <%   if (numPage <= 1) {
                           out.print("?where=" + sTable + "&p=" + (1) + "");
                       } else {
                           out.print("?where=" + sTable + "&p=" + (numPage - 1) + "");
                       }
                   %>
                   class="prev">&lt;</a>

                <%
                    out.print(Database.GetData.numPage(sTable, (numPage)));
                %>
                <a href=
                   <%
                       if (numPage >= ((Database.GetData.numRow(sTable) + 1)/10) + 1) {
                           out.print("?where=" + sTable + "&p=" + (numPage) + "");
                       } else {
                           out.print("?where=" + sTable + "&p=" + (numPage + 1) + "");
                       }
                   %>
                   class="next">&gt;</a>
            </nav>
        </section>
    </body>
</html>
